package gui;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import logic.AlchemikaGenerator;

public class Reactive {
	private AlchemikaGenerator Generator;

	/**
	 * Konstruktor
	 */
	public Reactive(AlchemikaGenerator Generator) {
		this.Generator = Generator;
	}
	
	public void addToTree(int pos, String name) {
		DefaultTreeModel model = (DefaultTreeModel) this.Generator.getGUIPresentation().tree.getModel();
		DefaultMutableTreeNode parent = null;
		switch(pos) {
		case 0: // root
			parent = (DefaultMutableTreeNode) model.getRoot();
			break;
		default: // Alchemikum | Charaktere | others
			if(pos>0) {
				parent = (DefaultMutableTreeNode) model.getChild(model.getRoot(), pos-1);
			}

		}
  
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(name);
		  
		//It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
		if(parent!= null) {
			model.insertNodeInto(childNode, parent, model.getChildCount(parent));

			this.Generator.getGUIPresentation().tree.scrollPathToVisible(new TreePath(childNode.getPath()));
		} else {
			System.out.println("Reactive::addToTree: Variable pos misused: "+pos);
		}
	}
	
	public void collapseTree() {
		DefaultTreeModel model = (DefaultTreeModel) this.Generator.getGUIPresentation().tree.getModel();
		DefaultMutableTreeNode firstChild = (DefaultMutableTreeNode) model.getChild(model.getRoot(), 0);
		this.Generator.getGUIPresentation().tree.collapsePath(new TreePath(firstChild.getPath()));
	}
}
